<?php
	ob_start();
	session_start();

	function __autoload($class_name) {
	include './'.$class_name . '.php';
	}
	$generatedOrderId = NULL;
	$buyerFirstname = $_GET['buyerFirstname'];
	HambaaUtils::validateUserInput($buyerFirstname);
	$buyerLastname = $_GET['buyerLastname'];
	HambaaUtils::validateUserInput($buyerLastname);
	$buyerAddress1 = $_GET['buyerAddress1'];
	HambaaUtils::validateUserInput($buyerAddress1);
	$buyerAddress2 = $_GET['buyerAddress2'];
	HambaaUtils::validateUserInput($buyerAddress2);
	$buyerCity = $_GET['buyerCity'];
	HambaaUtils::validateUserInput($buyerCity);
	$buyerState = $_GET['buyerState'];
	HambaaUtils::validateUserInput($buyerState);
	$buyerPincode = $_GET['buyerPincode'];
	HambaaUtils::validateUserInput($buyerPincode);
	$buyerEmail = $_GET['buyerEmail'];
	HambaaUtils::validateUserInput($buyerEmail);
	$buyerContactno = $_GET['buyerContactno'];
	HambaaUtils::validateUserInput($buyerContactno);
	$buyerBillFirstname = $_GET['buyerBillFirstname'];
	HambaaUtils::validateUserInput($buyerBillFirstname);
	$buyerBillLastname = $_GET['buyerBillLastname'];
	HambaaUtils::validateUserInput($buyerBillLastname);
	$buyerBillAddress1 = $_GET['buyerBillAddress1'];
	HambaaUtils::validateUserInput($buyerBillAddress1);
	$buyerBillAddress2 = $_GET['buyerBillAddress2'];
	HambaaUtils::validateUserInput($buyerBillAddress2);
	$buyerBillCity = $_GET['buyerBillCity'];
	HambaaUtils::validateUserInput($buyerBillCity);
	$buyerBillState = $_GET['buyerBillState'];
	HambaaUtils::validateUserInput($buyerBillState);
	$buyerBillPincode = $_GET['buyerBillPincode'];
	HambaaUtils::validateUserInput($buyerBillPincode);
	$buyerBillEmail = $_GET['buyerBillEmail'];
	HambaaUtils::validateUserInput($buyerBillEmail);
	$buyerBillContactno = $_GET['buyerBillContactno'];
	HambaaUtils::validateUserInput($buyerBillContactno);
	
	$couponCode = null;
	$isGift = "N";
	$status = true;
	$orderStatus = "N";
	$couponStatus = "N";
	$amount = 0;
	$results = array();
	$cart = $_SESSION['cart'];
	if(is_null($cart) || $cart -> countOrder() == 0){
	$status = false;		
	}else{

		$orders = "";
			
		$orderList = $cart -> getOrderList();		
		$cartKeys = array_keys($orderList);
			
				
		foreach($cartKeys as $key) {
			$orders = $orders . "(" . $key .",".$orderList[$key]->getQuantity()."),";
		}
		
		if($cart -> isGift()){
			$isGift="Y";
		}
		$couponCode = $cart -> getCouponCode();
			
		$orders = substr($orders, 0 , -1);
			
					

		$dbConfig = new DBConfigNew(); 
		$mysqli = $dbConfig->getConnection();

		if ($mysqli->connect_errno) {
			$status = false;
			
		}

		if ($status && !$mysqli->query("DROP TABLE IF EXISTS hambaaco_temp.details_order") ||
			!$mysqli->query("create TEMPORARY TABLE hambaaco_temp.details_order(product_id BIGINT(50),quantity INT(5))") ||
			!$mysqli->query("INSERT INTO hambaaco_temp.details_order(product_id,quantity) VALUES ".$orders)) {
			$status = false;
			echo 'here';
		}


		if ($status && !$mysqli->query("CALL hambaaco_merchandize.order_i('".$buyerFirstname."','".$buyerLastname."','".$buyerAddress1."','".$buyerAddress2."','".$buyerCity."','".$buyerState."','".$buyerPincode."','".$buyerEmail."','".$buyerContactno."',@orderid,'".$buyerBillFirstname."','".$buyerBillLastname."','".$buyerBillAddress1."','".$buyerBillAddress2."','".$buyerBillCity."','".$buyerBillState."','".$buyerBillPincode."','".$buyerBillEmail."','".$buyerBillContactno."','".$isGift."','".$couponCode."',@is_valid,@is_coupon_valid, @tx_final_amount)")) {
			$status = false;
			 die( "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error);
			echo 'in here';
		}

		if ($status){
			$orderOut=$mysqli->query( 'select @orderid,@is_valid,@is_coupon_valid,@tx_final_amount' );
			while($row = $orderOut->fetch_object()){
				$generatedOrderId = $row->{'@orderid'};
				$orderStatus = $row->{'@is_valid'};
				$couponStatus = $row->{'@is_coupon_valid'};				
				$amount = $row->{'@tx_final_amount'};	
			}
		}	
		$dbConfig -> closeConnection();

	}
	if($couponStatus == "N"){
		$cart -> setCouponCode(null);
		$status = false;
		$generatedOrderId = null;
	}
	if($orderStatus == "N"){
		$status = false;
		$generatedOrderId = null;
	}


		$cart->setOrderId($generatedOrderId);
		$results['#success'] = true;
		$results['status'] = $status;
		$results['orderId'] = $generatedOrderId;
		$results['orderStatus'] = $orderStatus;
		$results['couponStatus'] = $couponStatus;
		$results['amount'] = $amount;

	echo json_encode($results);
	ob_end_flush();
?>